package com.example.exammenudemo1;

import android.os.Bundle;
import android.app.Activity;
import android.text.Html;
import android.text.Spanned;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.View;
import android.widget.EditText;

public class MenuDemo1 extends Activity {
	EditText etMessage1;
	EditText etMessage2;
	Integer[] arrayPointSize = {10, 20, 30, 40, 50};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		etMessage1 = (EditText) findViewById(R.id.etMessage1);
		etMessage2 = (EditText) findViewById(R.id.etMessage2);
		
		etMessage1.setText("Thoai");
		etMessage2.setText("FA");
		//you may register an individual context menu for each view
		registerForContextMenu(etMessage1);
		registerForContextMenu(etMessage2);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {		
		populateMyFirstMenu(menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
		
		super.onCreateContextMenu(menu, v, menuInfo);
		
		//decide what context menu needs to be made 
		if(v.getId() == etMessage1.getId()){
			//Create a menu for etMessage1 box
			populateMyFirstMenu(menu);
		}
		if(v.getId() == etMessage2.getId()){
			//Create a menu for etMessage2 box
			populateMySecondMenu(menu);
		}
	}
	
	public boolean onContextMenuItemSelected(MenuItem item){
		return (applyMenuOption(item) || super.onContextItemSelected(item));
	}
	
	public boolean onOptionMenuItemSelected(MenuItem item){
		return (applyMenuOption(item) || super.onOptionsItemSelected(item));
	}
	
	private void populateMyFirstMenu(Menu menu){
		int groupId = 0;
		
		MenuItem item1 = menu.add(groupId, 1, 1, "10 points");
		MenuItem item2 = menu.add(groupId, 2, 2, "20 points");
		MenuItem item3 = menu.add(groupId, 3, 3, "30 points");
		MenuItem item4 = menu.add(groupId, 4, 4, "40 points");
		
		MenuItem item6 = menu.add(groupId, 6, 8, "Red text");
		MenuItem item7 = menu.add(groupId, 7, 7, "Green text");
		MenuItem item8 = menu.add(groupId, 8, 6, "Blue text");
		
		//set icons
		/*
		item1.setIcon(R.drawable.superman);
		item2.setIcon(R.drawable.tweetdeck);
		item3.setIcon(R.drawable.twicca);
		item4.setIcon(R.drawable.twidroyd);
		*/
		
		//Shortcuts using device's keyboard-keypad
		item1.setShortcut('1', '1');
		item2.setShortcut('2', '2');
		item3.setShortcut('3', '3');
		item4.setShortcut('4', '4');
		
		//adding a sub-menu as fifth entry of this menu 
		int smGroupId = 0; //don't care, same as Menu.NONE
		int smItemId = 5; //fifth element
		int smOrder = 5; //don't care, same as Menu.NONE
		
		SubMenu mySubMenu5 = menu.addSubMenu(smGroupId, smItemId, smOrder, "Sub-Menu-THOAI");
		//mySubMenu5.setHeaderIcon(R.drawable.twitter);
		//mySubMenu5.setIcon(R.drawable.superman);
		
		MenuItem sub51 = mySubMenu5.add(smGroupId, 1, 1, "Sub Menu 5-1");
		MenuItem sub52 = mySubMenu5.add(smGroupId, 2, 2, "Sub Menu 5-2");
		MenuItem sub53 = mySubMenu5.add(smGroupId, 3, 3, "Sub Menu 5-3");
	}
	
	private boolean applyMenuOption (MenuItem item){
		
		int menuItemId = item.getItemId();
		
		String strMsg2 = etMessage2.getText().toString();
		
		if(menuItemId < 5 ){
			//first four options are for setting text Size
			int newPointSize = arrayPointSize[menuItemId - 1];
			etMessage1.setTextSize(newPointSize);
			etMessage2.setTextSize(newPointSize);
		}
		else if (menuItemId == 5){
			//the Sub-Menu (attached to 5th item) is processed here
			etMessage1.setText("You have selected: \n" +item.getTitle()
								+"\nId: "+menuItemId
								+"order: "+item.getOrder());
		}
		
		//either change color on text1 or style on text2
		else if (menuItemId == 6){
			etMessage1.setTextColor(0xffff0000);//red
			etMessage1.setText("Thoai");
			
		}
		else if( menuItemId == 7)
			etMessage1.setTextColor(0xff00ff00);//green
		else if(menuItemId == 8)
			etMessage1.setTextColor(0xff0000ff);//blue
		else if(menuItemId == 9)
			etMessage2.setText(beautify(strMsg2, "BOLD"));//bold
		else if(menuItemId == 10)
			etMessage2.setText(beautify(strMsg2, "ITALIC"));//italic
		else if(menuItemId == 11)
			etMessage2.setText(beautify(strMsg2, "NORMAL"));//normal
		return true;
	}
	
	//changing text style using HTML formating
	//Spanned is text to which you could add formatting features
	private Spanned beautify (String originalText, String selectedStyle){
		Spanned answer = null;
		if(selectedStyle.equals("BOLD"))
			answer = Html.fromHtml("<b>" + originalText + "</b");
		else if(selectedStyle.equals("ITALIC"))
			answer = Html.fromHtml("<i>" +originalText + "</i>");
		else if(selectedStyle.equals("NORMAL"))
			answer = Html.fromHtml("<normal>" + originalText +"</normal");
		
		return answer;
	}
	
	private void populateMySecondMenu(Menu menu){
		int groupId = 0;
		int order = 0;
		menu.add(groupId, 9, 1, "Bold");
		menu.add(groupId, 10, 2, "Italic");
		menu.add(groupId, 11, 3, "Normal");
	}
}
